package com.bah.tract.client.ui;

import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Set;

import javax.swing.JDialog;

import com.bah.tract.client.ErrorHandler;
import com.bah.tract.client.workstore.tol.TOLClient;
import com.bah.tract.client.workstore.tol.TOLWork;
import com.bah.tract.client.workstore.tol.TOLWorkStore;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.inject.name.Named;

@Singleton
public class TOLSubmitDialog extends JDialog implements DataDialog {

	private final TOLPanel panel;

	@Override
	public final void setVisible(final boolean b) {
		// Refresh the data in the panel if it is being displayed
		if (b) {
			refresh();
		}
		super.setVisible(b);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.bah.tract.client.ui.TimeCardDialog#refresh()
	 */
	@Override
	public final void refresh() {
		panel.refresh();
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private final TOLWorkStore mTolWorkLog;

	private final TOLClient mTolClient;

	@Inject
	public TOLSubmitDialog(@Named("defaultImage") final Image image,
			final TOLWorkStore tolWorkLog, final TOLClient tolClient) {
		mTolClient = tolClient;
		setTitle("TRACT - TOL Time Card");
		mTolWorkLog = tolWorkLog;
		panel = new TOLPanel();
		setIconImage(image);
		add(panel);
		pack();
	}

	public class TOLPanel extends javax.swing.JPanel {

		/**
		 * 
		 */
		private static final long serialVersionUID = -8907128140135418478L;

		/** Creates new form NewJPanel6 */
		public TOLPanel() {
			initComponents();
		}

		public final void refresh() {
			final Set<TOLWork> daysWork = mTolWorkLog.getWork();
			final Object[][] data = new Object[daysWork.size()][5];
			int i = 0;
			for (final TOLWork work : daysWork) {
				data[i][0] = work.getNumber();
				data[i][1] = work.getNumber();
				data[i][2] = work.getTaxCode();
				data[i][3] = work.isTeleWork();
				data[i][4] = work.getMinutes() / 15.0;
				i++;
			}
			jTable1.setModel(new javax.swing.table.DefaultTableModel(data,
					new String[] { "Description", "Charge Number",
							"Tax Location", "Telework", "Hours" }));
		}

		/**
		 * This method is called from within the constructor to initialize the
		 * form. WARNING: Do NOT modify this code. The content of this method is
		 * always regenerated by the Form Editor.
		 */
		// <editor-fold defaultstate="collapsed"
		// desc="Generated Code">//GEN-BEGIN:initComponents
		private void initComponents() {

			jScrollPane1 = new javax.swing.JScrollPane();
			jTable1 = new javax.swing.JTable();
			jButton1 = new javax.swing.JButton();
			jButton2 = new javax.swing.JButton();

			setName("Form"); // NOI18N

			jScrollPane1.setName("jScrollPane1"); // NOI18N

			jTable1.setName("jTable1"); // NOI18N
			jScrollPane1.setViewportView(jTable1);

			jButton1.setText("Cancel"); // NOI18N
			jButton1.setName("jButton1"); // NOI18N
			jButton1.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(final ActionEvent e) {
					TOLSubmitDialog.this.setVisible(false);
				}
			});

			jButton2.setText("Submit"); // NOI18N
			jButton2.setName("jButton2"); // NOI18N
			jButton2.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(final ActionEvent e) {
					try {
						mTolClient.logWork(mTolWorkLog.getWork());
						TOLSubmitDialog.this.setVisible(false);
					} catch (final IOException e1) {
						ErrorHandler.handleError(e1);
					}

				}
			});

			final javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
					this);
			setLayout(layout);
			layout.setHorizontalGroup(layout
					.createParallelGroup(
							javax.swing.GroupLayout.Alignment.LEADING)
					.addGroup(
							javax.swing.GroupLayout.Alignment.TRAILING,
							layout.createSequentialGroup()
									.addContainerGap()
									.addGroup(
											layout.createParallelGroup(
													javax.swing.GroupLayout.Alignment.TRAILING)
													.addComponent(
															jScrollPane1,
															javax.swing.GroupLayout.Alignment.LEADING,
															javax.swing.GroupLayout.DEFAULT_SIZE,
															380,
															Short.MAX_VALUE)
													.addGroup(
															layout.createSequentialGroup()
																	.addComponent(
																			jButton2)
																	.addPreferredGap(
																			javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																	.addComponent(
																			jButton1)))
									.addContainerGap()));
			layout.setVerticalGroup(layout
					.createParallelGroup(
							javax.swing.GroupLayout.Alignment.LEADING)
					.addGroup(
							javax.swing.GroupLayout.Alignment.TRAILING,
							layout.createSequentialGroup()
									.addContainerGap()
									.addComponent(
											jScrollPane1,
											javax.swing.GroupLayout.DEFAULT_SIZE,
											240, Short.MAX_VALUE)
									.addPreferredGap(
											javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
									.addGroup(
											layout.createParallelGroup(
													javax.swing.GroupLayout.Alignment.BASELINE)
													.addComponent(jButton1)
													.addComponent(jButton2))
									.addContainerGap()));
		}// </editor-fold>//GEN-END:initComponents

		// Variables declaration - do not modify//GEN-BEGIN:variables
		private javax.swing.JButton jButton1;
		private javax.swing.JButton jButton2;
		private javax.swing.JScrollPane jScrollPane1;
		private javax.swing.JTable jTable1;
		// End of variables declaration//GEN-END:variables

	}

}
